package com.example.daiyinuo.composite.component;

import java.util.ArrayList;
import java.util.List;

public class OrganizationComposite extends OrganizationComponent {
    //很关键 这体现了组合的思想

    private List<OrganizationComponent> organizations = new ArrayList<OrganizationComponent>();

    public OrganizationComposite(String name) {
        super(name);
    }

    @Override
    public void add(OrganizationComponent organizationComponent) {
        organizations.add(organizationComponent);
    }

    @Override
    public OrganizationComponent getChild(String name) {
        for (OrganizationComponent org : organizations
        ) {
            OrganizationComponent targetOrg = org.getChild(name);
            if (targetOrg != null)
                return targetOrg;

        }
        return null;
    }

    @Override
    public int getStaffCount() {
        int count = 0;
        for (OrganizationComponent org : organizations) {
            count += org.getStaffCount();
        }
        return count;
    }
}
